﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="DGPLADES_AdminGrupo.aspx.cs" %>

<%@ Register assembly="DevExpress.Web.ASPxEditors.v11.2, Version=11.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxGridView.v11.2, Version=11.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxGridView" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.v11.2, Version=11.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxDataView" tagprefix="dx" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        // <![CDATA[
        function ASPxGridViewGrupos_SelectionChanged(s, e) {
            s.GetSelectedFieldValues("id_Grupos", GetSelectedFieldValuesCallback);
        }

        function GetSelectedFieldValuesCallback(values) {
            selList.BeginUpdate();
            try {
                selList.ClearItems();
                for (var i = 0; i < values.length; i++) {
                    selList.AddItem(values[i]);
                }
            } finally {
                selList.EndUpdate();
            }
            document.getElementById("selCount").innerHTML = grid.GetSelectedRowCount();
        }
          // ]]> 
    </script>
    <script runat="server">
        String grupoSeleccionado;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((IsPostBack))
            //{
            //    lblGrupo.Text = "SI es PostBack";
            // }
            //else
            //{
            //    lblGrupo.Text = "NO es PostBack";
            //}
            if (!IsPostBack)
            {
                grupoSeleccionado           = "1";
                ActualizaRolesNoAsignados(grupoSeleccionado);
                this.lblMensajeGrupo.Text = " Error al validar datos ";

            }
        }

        protected void ActualizaRolesNoAsignados(String grupoSel)
        {

            //con.Open();

            //SqlCommand cmd = new SqlCommand("select UserName,FirstName,LastName,Location from UserDetails", con);

            //SqlDataAdapter da = new SqlDataAdapter(cmd);

            String mysql_ConnS = Session.Contents["mysql_ConnectionString"].ToString();

            System.Data.Odbc.OdbcConnection mysqlconn_Conexion = new System.Data.Odbc.OdbcConnection(mysql_ConnS);

            System.Data.Odbc.OdbcCommand mysqlcmd_RolNoAsig = new System.Data.Odbc.OdbcCommand("SELECT * FROM Grupos " +
                " INNER JOIN Usuarios ON Grupos.id_Grupos = Usuarios.id_Grupos WHERE Grupo = ? and Username = ? ", mysqlconn_Conexion);

            try
            {
                //DataSet ds = new DataSet();
                //da.Fill(ds);
                //if (ds.Tables[0].Rows.Count == 0)
                //{
                //    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                //    gvdata.DataSource = ds;
                //    gvdata.DataBind()
                //    int columncount = gvdata.Rows[0].Cells.Count;
                //    gvdata.Rows[0].Cells.Clear();
                //    gvdata.Rows[0].Cells.Add(new TableCell());
                //    gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
                //    gvdata.Rows[0].Cells[0].Text = "No Records Found";
                //}
                //else
                //{
                //    gvdata.DataSource = ds;
                //    gvdata.DataBind();
                //}
                //con.Close();
                
                System.Data.Odbc.OdbcParameter mysqlparam_RolNoAsig = mysqlcmd_RolNoAsig.Parameters.Add("@str_IdGrupo", System.Data.Odbc.OdbcType.VarChar, 100);
                mysqlparam_RolNoAsig = mysqlcmd_RolNoAsig.Parameters.Add("@str_Username", System.Data.Odbc.OdbcType.VarChar, 100);
                
                
               System.Data.DataSet mysqlds = new System.Data.DataSet();                
               System.Data.Odbc.OdbcDataAdapter mysqlda = new System.Data.Odbc.OdbcDataAdapter(mysqlcmd_RolNoAsig);
                
               mysqlda.Fill(mysqlds);
                
                if (mysqlds.Tables[0].Rows.Count == 0)
                {
                    mysqlds.Tables[0].Rows.Add(mysqlds.Tables[0].NewRow());
                    ASPxGridViewRolNoAsig.DataSource = mysqlds;
                    ASPxGridViewRolNoAsig.DataBind();
                    int columncount         = ASPxGridViewRolNoAsig.Columns.Count;
                    ASPxGridViewRolNoAsig.Columns.Clear();
                    
                    GridViewDetailRowTemplateContainer container = ASPxGridViewRolNoAsig.NamingContainer as GridViewDetailRowTemplateContainer;
                    Int32 index = container.VisibleIndex;
                   
                    object str_hola = ASPxGridViewRolNoAsig.Columns.Grid.GetRowValues(index, "id_Roles");
                    //ASPxGridViewRolNoAsig.GetRow(0).Cells.Add(new TableCell());
                    //ASPxGridViewRolNoAsig.GetRow(0).Cells[0].ColumnSpan = columncount;
                    //ASPxGridViewRolNoAsig.GetRow(0).Cells[0].Text = "Sin roles qu&eacute; mostrar";
                }
                else
                {
                     ASPxGridViewRolNoAsig.DataSource = mysqlds;
                     ASPxGridViewRolNoAsig.DataBind();
                }
 
            }
            catch (Exception ex)
            {
                Session.Contents["ses_bl_entrada"] = "0";
                throw new Exception("Error al obtener datos del Grupo. " + ex.Message);
             }
        }
           
        protected void ButtonGuardar_Click(object sender, EventArgs e)
        {
            // Se re-direcciona hacia la pagina principal.
            Session.Contents["ses_intnivel"] = 0;
            Response.Redirect("Default.aspx", false);
        }

        protected void ButtonLimpiar_Click(object sender, EventArgs e)
        {
            // Se re-direcciona hacia la pagina principal.
            Session.Contents["ses_intnivel"] = 0;
            Response.Redirect("Default.aspx", false);
        }

        protected void ButtonAsociar_Click(object sender, EventArgs e)
        {
            // Se re-direcciona hacia la pagina principal.
            Session.Contents["ses_intnivel"] = 0;
            Response.Redirect("Default.aspx", false);
        }

        protected void ButtonAgregar_Click(object sender, EventArgs e)
        {
            // Se re-direcciona hacia la pagina principal.
            Session.Contents["ses_intnivel"] = 0;
            Response.Redirect("Default.aspx", false);
        }
    
        protected void ButtonExcluir_Click(object sender, EventArgs e)
        {
            // Se re-direcciona hacia la pagina principal.
            Session.Contents["ses_intnivel"] = 0;
            Response.Redirect("Default.aspx", false);
        }

        protected void ASPxGridViewGrupos_SelectionChanged(Object sender, EventArgs e)
        {
            // Se obtienen datos del Grupo seleccionado. 
            string[] strlista = { "id_Grupos", "Nombre", "Descripcion" };
            List<Object> row = ASPxGridViewGrupos.GetSelectedFieldValues(strlista);
//            GridViewRow row = (GridViewRow)ASPxGridViewRolNoAsig.GetRow(ASPxGridViewRolNoAsig.FocusedRowIndex);
        } 
    </script>
    <style type="text/css">
        .style3
        {
            width: 287px;
        }
        .style4
        {
            width: 217px;
        }
        .style5
        {
            width: 389px;
        }
        .style9
        {
            width: 849px;
        }
        .style10
        {
            height: 32px;
        }
        .style16
        {
            width: 162px;
        }
        .style18
        {
            width: 308px;
        }
        .style19
        {
            width: 764px;
        }
        .style20
        {
            width: 388px;
        }
        .style21
        {
            width: 358px;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table style="width:60%;" width="200">
        <tr valign="top">
            <td class="style9">
               <h2>
                   <asp:Label ID="lblGrupo" runat="server" Text="Grupos"></asp:Label>
               </h2>
            </td>
            <td colspan="3" class="style5" valign="top">
                <table style="width: 144%; height: 120px;">
                    <tr>
                        <td class="style19">
                            <table style="width:50%;">
                                <tr>
                                    <td class="style4">
                                        &nbsp;</td>
                                    <td class="style2">
                                        Clave:</td>
                                    <td class="style3">
                                        <asp:TextBox ID="TextBox_IdGrupo"  disabled="true" runat="server" Width="47px"></asp:TextBox>
                                    </td>
                                    <td class="style5" valign="top">
                                        <asp:Button ID="ButtonGuardar" runat="server" onclick="ButtonGuardar_Click" 
                                            Text="Guardar" Width="62px" />
                                            &nbsp;<asp:Button ID="ButtonGuardar0" runat="server" onclick="ButtonLimpiar_Click" 
                                            Text="Limpiar" Width="50px" />
                                            &nbsp;<asp:Button ID="ButtonGuardar1" runat="server" onclick="ButtonAsociar_Click" 
                                            Text="Asociar" Width="53px" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="style4">
                                        &nbsp;</td>
                                    <td class="style2">
                                        Nombre</td>
                                    <td class="style3">
                                            <asp:TextBox ID="TextBox_Nombre0"  runat="server" Height="23px" 
                                                Width="145px"></asp:TextBox>                                   
                                        </td>
                                    <td class="style18">
                                        &nbsp;</td>
                                </tr>
                                <tr>
                                    <td class="style4">
                                        &nbsp;</td>
                                    <td class="style2">
                                       Descripci&oacute;n:</td>
                                    <td class="style3">
                                        <asp:TextBox ID="TextBox_Contrasenia" runat="server" Height="22px" 
                                            Width="233px"></asp:TextBox>
                                    </td>
                                    <td class="style18">
                                        &nbsp;</td>
                                </tr>
                                <tr>
                                    <td class="style4">
                                        &nbsp;</td>
                                    <td class="style2">
                                        &nbsp;</td>
                                    <td class="style3">
                                        
                                    </td>
                                    <td class="style18">
                                        &nbsp;</td>
                                </tr>
                            </table>
                        </td>
                   </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="4" class="style19">
                <dx:ASPxGridView ID="ASPxGridViewGrupos" runat="server" AutoGenerateColumns="False" 
                    DataSourceID="SqlDataSourceGrupo" AllowSelectItem="True" onselectedindexchanged="ASPxGridViewGrupos_SelectionChanged"
                    Width="511px">
                    <%--<ClientSideEvents RowClick="function(s, e) { OnGrupoRow_Click(e); }"></ClientSideEvents>--%>
                    <%--<ClientSideEvents SelectionChanged="ASPxGridViewGrupos_SelectionChanged" />--%>
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="id_Grupos" 
                            VisibleIndex="0">
                            <EditFormSettings Visible="False" />
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="Nombre" 
                            VisibleIndex="1">
                        </dx:GridViewDataTextColumn>
                                <dx:GridViewDataTextColumn FieldName="Descripcion" 
                            VisibleIndex="2">
                        </dx:GridViewDataTextColumn>
                    </Columns>
                    <Settings ShowGroupPanel="True" ShowHorizontalScrollBar="True" />
                </dx:ASPxGridView>                                                   
            </td>                       
        </tr>
        <tr>
            <td colspan="4" class="style9">
                <asp:SqlDataSource ID="SqlDataSourceGrupo" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:DGPLADES_BudConnectionString %>"                    
                    SelectCommand="SELECT id_Grupos, Nombre, Descripcion FROM bud.grupos ORDER BY Nombre" 
                    ProviderName="<%$ ConnectionStrings:DGPLADES_BudConnectionString.ProviderName %>">
                </asp:SqlDataSource>
            </td>
        </tr>
        <tr valign="top">
            <td class="style9" valign="top">
                &nbsp;</td>
            <td class="style21" valign="top">
                            <dx:ASPxGridView ID="ASPxGridViewRolNoAsig" runat="server" AutoGenerateColumns="False"
                                AllowSelectItem="True" Width="362px">
                                <Columns>
                                    <dx:GridViewDataTextColumn FieldName="id_Roles" 
                                        VisibleIndex="1">
                                    </dx:GridViewDataTextColumn>
                                    <dx:GridViewDataTextColumn FieldName="Nombre" 
                                        VisibleIndex="2">
                                    </dx:GridViewDataTextColumn>
                                </Columns>
                                <Settings ShowGroupPanel="True" ShowHorizontalScrollBar="True" />
                            </dx:ASPxGridView>                        
<%--                            <asp:SqlDataSource ID="SqlDataSourceRol0" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:DGPLADES_BudConnectionString %>"                    
                                SelectCommand="SELECT * FROM bud.roles" 
                                
                    ProviderName="<%$ ConnectionStrings:DGPLADES_BudConnectionString.ProviderName %>">
                            </asp:SqlDataSource>--%>
            </td>
            <td class="style5" valign="top">
                                        <asp:Button ID="Button_Guardar" runat="server" onclick="ButtonAgregar_Click" 
                                            Text=">" Width="53px" />
                                            <br />
                                             <asp:Button ID="Button_Excluir" runat="server" onclick="ButtonExcluir_Click" 
                                            Text="<" Width="53px" />                                   
                                    </td>
            <td class="style5" valign="top">
                            <dx:ASPxGridView ID="ASPxGridViewRol1" runat="server" AutoGenerateColumns="False" 
                                DataSourceID="SqlDataSourceRol1" AllowSelectItem="True" 
                                Width="362px" style="margin-left: 0px">
                                <Columns>
                                    <dx:GridViewDataTextColumn FieldName="id_Roles" 
                                        VisibleIndex="1">
                                    </dx:GridViewDataTextColumn>
                                    <dx:GridViewDataTextColumn FieldName="Nombre" 
                                        VisibleIndex="2">
                                    </dx:GridViewDataTextColumn>
                                </Columns>
                                <Settings ShowGroupPanel="True" ShowHorizontalScrollBar="True" />
                            </dx:ASPxGridView>                        
                            <asp:SqlDataSource ID="SqlDataSourceRol1" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:DGPLADES_BudConnectionString %>"                    
                                SelectCommand="SELECT * FROM bud.roles"                     
                    ProviderName="<%$ ConnectionStrings:DGPLADES_BudConnectionString.ProviderName %>">
                            </asp:SqlDataSource>
                        </td>
        </tr>
        <tr>
            <td class="style9">
                &nbsp;</td>
                <td class="style21">
                    &nbsp;</td>
            <td colspan="2">
                <asp:Label ID="lblMensajeGrupo" runat="server" Text=""></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>
